/* comparison.cpp
Michael Zahniser, 12/12/09

Function definitions for the comparison operators.
*/

#include "Operators.h"

#include <algorithm>

using std::string;


namespace {
	string Equals = "equals eq ==";
	static const Do<Equals> equals;

	string NotEquals = "notequals noteq neq !=";
	static const Do<NotEquals> notequals;

	string Less = "less <";
	static const Do<Less> less;

	string LessEqual = "lessequal lesseq <=";
	static const Do<LessEqual> lesseq;

	string Great = "great >";
	static const Do<Great> great;

	string GreatEqual = "greatequal greateq >=";
	static const Do<GreatEqual> greatequal;

	string Minimum = "minimum min <?";
	static const Do<Minimum> minimum;

	string Maximum = "maximum max >=";
	static const Do<Maximum> maximum;
}



template <> void Do<Equals>::operator()(State &state) const
{
	state.Push(state.Pop() == state.Pop());
}



template <> void Do<NotEquals>::operator()(State &state) const
{
	state.Push(state.Pop() != state.Pop());
}



template <> void Do<Less>::operator()(State &state) const
{
	double b = state.Pop();
	state.Push(state.Pop() < b);
}



template <> void Do<LessEqual>::operator()(State &state) const
{
	double b = state.Pop();
	state.Push(state.Pop() <= b);
}



template <> void Do<Great>::operator()(State &state) const
{
	double b = state.Pop();
	state.Push(state.Pop() > b);
}



template <> void Do<GreatEqual>::operator()(State &state) const
{
	double b = state.Pop();
	state.Push(state.Pop() >= b);
}



template <> void Do<Minimum>::operator()(State &state) const
{
	state.Push(std::min(state.Pop(), state.Pop()));
}



template <> void Do<Maximum>::operator()(State &state) const
{
	state.Push(std::max(state.Pop(), state.Pop()));
}
